#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "lrc_lib.h"
#include "../pos/console.h"


char *get_text(char *file_name)
{
    FILE* text_rb = fopen(file_name,"rb");

    fseek(text_rb,0,SEEK_END);
    int len = ftell(text_rb);
    rewind(text_rb);

    char* text = (char*)malloc(len+1);
    memset(text,'\0',len+1);

    fread(text,len,1,text_rb);
    fclose(text_rb);

    return text;
}

void segmented_text(char *seg_text[],int *seg_len,char *text)
{
    seg_text[*seg_len] = strtok(text,"\r\n");
    while (seg_text[*seg_len] != NULL)
    {
        seg_text[++*seg_len] = strtok(NULL,"\r\n");
    }
}

void get_title(char *seg_text[],char *title[],int *len)
{
    while (strchr(seg_text[*len],']')-seg_text[*len] > 10)
    {
        title[*len] = (char*)malloc(strlen(seg_text[*len]));
        sscanf(seg_text[*len],"%*1s%[^]]",title[*len]);
        (*len)++;
    }
}

void get_link(char *seg_text[],int seg_len,int title_len,LRC** head)
{
    for (int i = title_len; i < seg_len; i++)
    {
        char * temp_str;
        temp_str = seg_text[i];
        float mins,sec;
        float temp_timelist[10];
        int time_num = 0;
        while (*temp_str == '[')
        {
            sscanf(temp_str,"[%f:%f]",&mins,&sec);
            temp_timelist[time_num] = mins*60 + sec;
            time_num++;
            temp_str += strchr(temp_str,']')-temp_str+1;
        }
        for (int j = 0; j < time_num; j++)
        {
            LRC *temp_node = (LRC*)malloc(sizeof(LRC));
            temp_node->time = temp_timelist[j];
            strcpy(temp_node->lrc_str,temp_str);
            doubly_link(head,temp_node);
        }
    }
}

void doubly_link(LRC **head,LRC *node)
{
    LRC *current = *head;
    if (current == NULL)
    {
        node->next = NULL;
        node->perv = NULL;
        *head = node;
        return;
    }
    while (current)
    {
        if(current->time > node->time)
        {
            if (current == *head)
            {
                node->next = current;
                current->perv = node;
                *head = node;
                return;
            }
            else
            {
                node->next = current;
                node->perv = current->perv;
                current->perv->next = node;
                current->perv = node;
                return;
            }
        }
        if(current->next == NULL&&current != node)
        {
            current->next = node;
            node->perv = current;
            node->next = NULL;
            return;
        }
            current = current->next;
    }
}

void printf_title(char *title[],int title_len)
{
    clear_screen();
    cusor_hide();
    set_fg_color(32);
    for (int i = 0; i < title_len; i++)
    {
        int x = strlen(title[i])/3*2;
        cusor_moveto((100-x)/2, i+1);
        printf("%s\n",title[i]);
    }
}

void printf_time(TIME time,int Y)
{
    char str[10];
    sprintf(str,"%02d : %02d",time.mins,time.sec);
    int x = strlen(str);
    printf_set(str,(100-x)/2+1,Y,34);
}

void printf_set(char* str,int X,int Y,int coloc)
{
    cusor_moveto(X, Y);
    set_fg_color(coloc);
    printf("%s\n",str);
}

void printf_first_fews(LRC *head,int Y)
{
    for (int i = 0; i < 5; i++)
    {
        int x = strlen(head->lrc_str)/3*2;
        printf_set(head->lrc_str,(100-x)/2,Y+i,32);
        head = head->next;
    }
}

void set_time(TIME *time)
{
    (*time).msec++;
    if ((*time).msec == 10)
    {
        (*time).sec++;
        (*time).msec = 0;
    }
    if ((*time).sec == 60)
    {
        (*time).sec = 0;
        (*time).mins++;
    }
    if ((*time).mins == 60)
    {
        (*time).mins = 0;
    }
}

LRC *find_current_node(LRC *head,float time)
{
    LRC *current = head;
    while (current)
    {
        if (current->time <= time && current->next != NULL && time < current->next->time)
        {
            return current;
        }
        else if (current->time <= time && current->next == NULL)
        {
            return current;
        }
        current = current->next;
    }
    return NULL;    
}

LRC* get_printf_node(LRC *current_node,LRC* head)
{
    if (current_node == head)return current_node;
    else if (current_node->perv == head)return current_node->perv;
    else if (current_node->next->next == NULL)return current_node->perv->perv->perv->perv;
    else if (current_node->next->next->next == NULL)return current_node->perv->perv->perv;
    else return current_node->perv->perv;
}

void printf_lrc(LRC *current_node,LRC *printf_node,int Y)
{
    LRC *temp = printf_node;
    for (int i = 0; i < 5; i++)
    {
        int x = strlen(temp->lrc_str)/3*2;
        if (temp == current_node) printf_set(temp->lrc_str,(100-x)/2,Y+i,36);
        else printf_set(temp->lrc_str,(100-x)/2,Y+i,32);
        temp = temp->next;
        //if (temp == NULL)return;
    }
}

void doubly_link_free(LRC *head)
{
    LRC* temp;
    while (head != NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }
}

void title_free(char *text[],int len)
{
    for (int i = 0; i < len; i++)
    {
        if (text[i] != NULL)
        {
            free(text[i]);
        }
    }
}



